var gulp = require('gulp');
var webpack = require('gulp-webpack');
var sequence = require('gulp-sequence');
var connect = require('gulp-connect');
var del = require('del');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');

gulp.task('webpack',function(){
    return gulp.src('./src/js/**/*.js')
        .pipe(webpack(require('./webpack.config.js')))
        .pipe(gulp.dest('./build/js'));
})

.task('watch',function(){
    gulp.watch('./src/js/**/*.js',['webpack']);
    gulp.watch('./src/sass/*.scss',['sass','webpack']);
    gulp.watch('./src/*.html',['move']);
    gulp.watch('./src/images/**/*',['move']);
})

.task('move',function(){
    gulp.src('./src/*.html').pipe(gulp.dest('./build'));
    gulp.src('./src/images/**').pipe(gulp.dest('./build/images'));
})

.task('server',function(){
    connect.server({
        port:3000,
        root: ['./build'],
        livereload: true
    });
})

.task('clean',function(){
    del.sync('./build');
})

.task('sass', function () {
    gulp.src('./src/sass/*.scss').pipe(sass().on('error', sass.logError)).pipe(gulp.dest('./build/css'));
    gulp.src('./src/sass/*.scss').pipe(sass().on('error', sass.logError)).pipe(gulp.dest('./src/css'));
})

.task('uglify',function(){
    gulp.src('./src/support/*.js').pipe(uglify()).pipe(gulp.dest('./build/support'));
})

.task('default',sequence('clean',['sass','webpack','move','uglify','server'],'watch'))

.task('build',sequence('clean',['sass','webpack','uglify','move']));


